using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DM.DAO;

namespace DM.Web.Admin.QuanLyThanhVien
{
    public partial class DanhSachThanhVien : System.Web.UI.Page
    {
        readonly NsdDAO nsdService = new NsdDAO();

        protected void Page_Load(object sender, EventArgs e)
        {
           BindingGridViewNSD();
        }


        /// <summary>
        /// Gắn kết dữ liệu vào gridview
        /// </summary>
        private void BindingGridViewNSD()
        {
            DataSet dataSet = nsdService.Get();
            DataView nsdDataView = dataSet.Tables[0].DefaultView;
            nsdDataView.Sort = ShortColumn + " " + (ShortAscend ? "ASC" : "DESC");
            GridViewNSD.DataSource = nsdDataView;
            GridViewNSD.DataBind();
        }

        //Property de short Grid
        private string ShortColumn
        {
            get
            {
                object o = Session["ShortColumn"];
                if (o == null) return "TenDangNhap";
                return (string) o;
            }
            set { Session["ShortColumn"] = value; }
        }
        private bool ShortAscend
        {
            get 
            { 
                object o = Session["ShortAscend"];
                if (o == null) return true;
                return (bool) o;
            }
            set { Session["ShortAscend"] = value; }
        }

        protected void GridViewNSD_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (e.SortExpression == ShortColumn) ShortAscend = !ShortAscend;
            else
            {
                ShortColumn = e.SortExpression;
                ShortAscend = true;
            }
            BindingGridViewNSD();
        }

        protected void ButtonXoa_Click(object sender, EventArgs e)
        {
            Button button = (Button) sender;
            GridViewRow currentRow = (GridViewRow) button.Parent.Parent;
            GridViewNSD.EditIndex = currentRow.RowIndex;
            GridViewNSD.DataBind();
        }

        protected void ButtonDel_Click(object sender, EventArgs e)
        {
            Button button = (Button) sender;
            GridViewRow currentRow = (GridViewRow) button.Parent.Parent;
            string nsdID = GridViewNSD.DataKeys[currentRow.RowIndex].Value.ToString();
            nsdService.Delete(nsdID);
            GridViewNSD.DeleteRow(currentRow.RowIndex);
            BindingGridViewNSD();
        }

        protected void ButtonNoDel_Click(object sender, EventArgs e)
        {
            GridViewNSD.EditIndex = -1;
        }

        protected void GridViewNSD_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewNSD.EditIndex = -1;
            GridViewNSD.DataBind();
        }

        protected void GridViewNSD_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewNSD.PageIndex = e.NewPageIndex;
            GridViewNSD.DataBind();
        }

        protected void ButtonEdit_Click(object sender, EventArgs e)
        {
            Button button = (Button) sender;
            GridViewRow currentRow = (GridViewRow) button.Parent.Parent;
            string maNsd = GridViewNSD.DataKeys[currentRow.RowIndex].Value.ToString();
            Response.Redirect("ThongTinThanhVien.aspx?id="+maNsd);
        }

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThemMoiThanhVien.aspx");
        }

    }
}
